package com.project.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.music.bean.Collect;
import com.project.music.service.CollectService;
import com.project.music.utils.Consts;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 收藏控制类
 */

@Api(tags = "收藏控制类")
@RestController
@RequestMapping("/collection")
public class CollectController {

    @Autowired
    CollectService collectService;

    //返回所有用户收藏列表
    @ApiOperation("返回所有用户收藏列表")
    @GetMapping("")
    public List<Collect> allCollection() {
        return collectService.list();
    }
    //返回指定id用户的收藏列表
    @ApiOperation("返回指定id用户的收藏列表")
    @GetMapping("/detail")
    public List<Collect> collectionOfId(@RequestParam("userId") @ApiParam("用户ID") int userId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(Collect::getUserId, userId);
        return collectService.list(queryWrapper);
    }
    //添加收藏歌曲
    @ApiOperation("添加收藏歌曲")
    @PostMapping("/add")
    public JSONObject addCollection(Collect collect) {
        JSONObject jsonObject = new JSONObject();

        if (collect.getSongId() == 0) {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "该歌曲未收录");
            return jsonObject;
        } else if (collectService.existSong(collect.getUserId(), collect.getSongId())) {
            jsonObject.put(Consts.CODE, 2);
            jsonObject.put(Consts.MSG, "已收藏");
            return jsonObject;
        }
        collect.setCreateTime(new Date());

        boolean status = collectService.save(collect);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "收藏成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "收藏失败");
        }
        return jsonObject;
    }
    //删除收藏歌曲
    @ApiOperation("删除收藏歌曲")
    @GetMapping("/delete")
    public Boolean deleteCollection(@RequestParam("userId") @ApiParam("用户ID") int userId,
                                   @RequestParam("songId") @ApiParam("歌曲ID") int songId) {
        QueryWrapper<Collect> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                    .eq(Collect::getUserId, userId)
                    .eq(Collect::getSongId, songId);
        return collectService.remove(queryWrapper);
    }
    //更新收藏
    @ApiOperation("更新收藏")
    @PostMapping("/update")
    public JSONObject updateCollection(Collect collect) {
        JSONObject jsonObject = new JSONObject();

        boolean status = collectService.save(collect);
        if (status) {
            jsonObject.put(Consts.CODE, 1);
            jsonObject.put(Consts.MSG, "修改成功");
        } else {
            jsonObject.put(Consts.CODE, 0);
            jsonObject.put(Consts.MSG, "修改失败");
        }
        return jsonObject;
    }
}